package cn.flying.cloud.gateway.route.controller;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import reactor.core.publisher.Mono;

import cn.flying.cloud.base.common.vo.PageRequest;
import cn.flying.cloud.base.common.vo.PageResponse;
import cn.flying.cloud.base.common.vo.Rt;
import cn.flying.cloud.gateway.route.GatewayRefreshRouteHandler;
import cn.flying.cloud.gateway.route.model.GatewayRoute;
import cn.flying.cloud.gateway.route.service.GatewayRouteService;

/**
 * @author: admin
 * @date: 2024年05月08日 17:26
 * @version: 1.0
 */
@RestController
@RequestMapping("/api/gateway")
public class GatewayRouteController {

    @Resource
    private GatewayRouteService gatewayRouteService;
    @Resource
    private GatewayRefreshRouteHandler gatewayRefreshRouteHandler;

    /**
     * 分页查询路由信息
     *
     * @param request
     * @return
     */
    @RequestMapping("page")
    public Mono<PageResponse<GatewayRoute>> page(@RequestBody PageRequest<GatewayRoute> request) {
        PageResponse response = gatewayRouteService.page(request.getPageNum(), request.getPageSize(), request.getParam());
        return Mono.just(response);
    }

    /**
     * 保存路由配置
     *
     * @param route
     * @return
     */
    @RequestMapping("save")
    public Mono<Rt> save(@RequestBody GatewayRoute route) {
        gatewayRouteService.save(route);
        return Mono.just(Rt.success("保存成功！"));
    }

    /**
     * 删除路由配置
     *
     * @param route
     * @return
     */
    @RequestMapping("delete")
    public Mono<Rt> delete(GatewayRoute route) {
        gatewayRouteService.delete(route);
        return Mono.just(Rt.success("删除成功！"));
    }

    /**
     * 刷新路由配置
     *
     * @param
     * @return
     */
    @RequestMapping("refresh")
    public Mono<Rt> refresh() {
        gatewayRefreshRouteHandler.refreshRoutes();
        return Mono.just(Rt.success("刷新成功！"));
    }
}
